<template>
  <div class="xtx-login-page">
    <!-- 登录头部 -->
    <div class="xtx-login-header">
      <h1 class="logo"></h1>
      <router-link class="home" to="/">
        进入网站首页
        <i class="iconfont icon-angle-right"></i>
        <i class="iconfont icon-angle-right"></i>
      </router-link>
    </div>
    <!-- 登录内容 -->
    <div class="qq-login">
      <section class="container">
        <div class="unbind" v-if="isBind">
          <div class="loading"></div>
        </div>
        <nav class="tab">
          <a @click="hasAccount=true" :class="{active:hasAccount}" href="javascript:;">
            <i class="iconfont icon-bind" />
            <span>已有小兔鲜账号，请绑定手机</span>
          </a>
          <a @click="hasAccount=false" :class="{active:!hasAccount}" href="javascript:;">
            <i class="iconfont icon-edit" />
            <span>没有小兔鲜账号，请完善资料</span>
          </a>
        </nav>
        <div class="tab-content" v-if="hasAccount">
          <CallbackBind :nickname="nickname" :avatar="avatar" :openId="openId" />
        </div>
        <div class="tab-content" v-else>
          <CallbackPatch :openId="openId"/>
        </div>
      </section>
    </div>
    <!-- 登录底部 -->
    <div class="xtx-login-footer">
      <div class="copyright">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; 小兔鲜儿</p>
      </div>
    </div>
  </div>
</template>

<script>
import CallbackBind from './components/callback-bind.vue'
import CallbackPatch from './components/callback-patch.vue'
import QC from 'qc'
import { mapMutations } from 'vuex'
import defaultAvatar from '@/assets/images/200.png'
export default {
  name: 'LoginCallback',
  components: {
    CallbackBind,
    CallbackPatch
  },
  data () {
    return {
      nickname: '',
      avatar: defaultAvatar,
      hasAccount: true,
      isBind: true,
      openId: ''
    }
  },
  methods: {
    ...mapMutations('user', ['setUser'])
  },
  created () {
    if (QC.Login.check()) {
      QC.Login.getMe(async (openId) => {
        this.openId = openId
        try {
          // 已注册，已绑定
          const { data } = await this.$http.post('/login/social', { unionId: openId, source: 6 })
          this.setUser(data.result)
          this.$router.push('/')
        } catch (e) {
          this.isBind = false
          // 未绑定
          QC.api('get_user_info').success(({ data }) => {
            this.avatar = data.figureurl_1
            this.nickname = data.nickname
          })
        }
      })
    }
  }
}
</script>

<style scoped lang='less'>
@import "./styles/login.less";
.qq-login {
  background: #f5f5f5;
}
.container {
  padding: 25px 0;
  position: relative;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../assets/images/load.gif) no-repeat center / 100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
